﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Collections.Generic;

namespace Geleximco.Common {
    /// <summary>
    /// Summary description for Hash
    /// </summary>
    public class HashPassHelper {
        private const string LIST_CHARS = "0Aa1Bb2Cc3Dd4Ee5Ff6Gg7Hh8Ii9Jj$Kk&Ll#Mm?NnOoPpQqRrSsTtUuWwVvXxYyZz";
        private const int NUMBER_OF_CHARS_PART = 9;

        public HashPassHelper() {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Hash password, return pass after hashed and sault in a dictionary
        /// </summary>
        /// <param name="pass"></param>
        /// <returns>1st string: pass after hashed. 2nd string: sault</returns>
        public static IDictionary<string, string> HashPass(string pass) {
            string sault = GenerateSault();
            string hashString = HashString(pass + sault);

            Dictionary<string, string> result = new Dictionary<string, string>();
            result.Add(hashString, sault);

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="checkedPass">Password need to checked</param>
        /// <param name="userHashedPass">Password of user get from DB</param>
        /// <param name="sault">Sault of user get from DB</param>
        /// <returns></returns>
        public static bool CheckRightPass(string checkedPass, string userHashedPass, string sault) {
            return (HashString(checkedPass + sault) == userHashedPass);
        }

        private static string HashString(string input) {
            byte[] b_input = System.Text.ASCIIEncoding.ASCII.GetBytes(input);
            HashAlgorithm hash = HashAlgorithm.Create("SHA1");
            try {
                byte[] result = hash.ComputeHash(b_input);
                String s = "";
                foreach (byte b in result) {
                    s += b.ToString("X2");
                }

                hash.Clear();
                return s;
            } catch (System.Exception) {
                hash.Clear();
                return "";
            }
        }

        private static string GenerateSault() {
            return Guid.NewGuid().ToString();
            //StringBuilder sault = new StringBuilder();
            //Random r = new Random();
            
            //for (int i = 0; i < NUMBER_OF_CHARS_PART; i++) {
            //    sault.Append(listChars[r.Next(0, listChars.Length - 1)]);
            //}

            //sault.Append(r.Next(100000000, int.MaxValue).ToString());
            //return sault.ToString();
        }
    } 
}
